import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  ManyToOne,
  JoinColumn,
  Unique,
  CreateDateColumn,
  UpdateDateColumn,
} from 'typeorm';

import { Punch, Food } from '@/modules/app/entities';
import { IsNumber, IsOptional, IsString } from 'class-validator';

@Entity('punch_food')
export class PunchFood {
  @PrimaryGeneratedColumn('increment')
  punchFoodId?: number;

  @IsNumber()
  @Column()
  punchId: number;

  @ManyToOne(() => Punch, (punch) => punch.punchFood, {
    onDelete: 'CASCADE',
  })
  @JoinColumn({ name: 'punchId' })
  punch?: Punch;

  @IsNumber()
  @Column()
  foodId: number;

  @ManyToOne(() => Food, (food) => food.punchFoods)
  @JoinColumn({ name: 'foodId' })
  food?: Food;

  /**
   * 食用量,多少份
   */
  @IsNumber()
  @Column({
    type: 'float',
    default: 0,
  })
  count: number;

  /**
   * 食用量,多少克
   */
  @IsNumber()
  @Column({
    type: 'float',
    default: 0,
  })
  quantity: number;

  /**
   * 单位
   * @example 盘
   * @example 份
   */
  @IsOptional()
  @IsString()
  @Column({
    default: null,
  })
  unit?: string;

  /**
   * 每单位食物的克数
   * @example 0.5
   * @example 100
   */
  @IsOptional()
  @IsNumber()
  @Column({ type: 'float' })
  unitWeight?: number = 0;

  @CreateDateColumn()
  createdAt?: Date;

  @UpdateDateColumn()
  updatedAt?: Date;
}
